package cn.org.seventh.web.common;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationContextException;
import org.springframework.context.support.MessageSourceAccessor;

/**
 * 能够被代理的CustomApplicationObjectSupport 行为同spring自带的ApplicationObjectSupport
 * 这里仅仅去掉了所有方法的final标识 使得该类方法在子类中被代理后该类方法也能被代理 从而使得该类的子类行为和原有类保持一致
 * 如果不去掉final标识会使得用代理类的引用调用非代理方法不能获取代理前的原始值 注意在被代理过得方法中的this引用的是代理前的对象
 * 未代理的final方法中的this引用是代理类对象 非final的方法中均可以this表示代理前对象 为了避免错误均采用一致行为
 * 在fianl标识的方法中凡是要保持和代理前行为一致可能需要改变this引用属性方式 解决办法为使用get set 如果原有属性没有 可能会添加访问方法
 * Convenient superclass for application objects that want to be aware of the
 * application context, e.g. for custom lookup of collaborating beans or for
 * context-specific resource access. It saves the application context reference
 * and provides an initialization callback method. Furthermore, it offers
 * numerous convenience methods for message lookup.
 * 
 * <p>
 * There is no requirement to subclass this class: It just makes things a little
 * easier if you need access to the context, e.g. for access to file resources
 * or to the message source. Note that many application objects do not need to
 * be aware of the application context at all, as they can receive collaborating
 * beans via bean references.
 * 
 * <p>
 * Many framework classes are derived from this class, particularly within the
 * web support.
 * 
 * @author Rod Johnson
 * @author Juergen Hoeller
 * @see org.springframework.web.context.support.WebApplicationObjectSupport
 * @see cn.org.seventh.web.common.CustomWebApplicationObjectSupport
 * @see org.springframework.context.support.ApplicationObjectSupport
 */
public abstract class CustomApplicationObjectSupport implements
		ApplicationContextAware {
	/** Logger that is available to subclasses */
	protected final Log logger = LogFactory.getLog(getClass());

	/** ApplicationContext this object runs in */
	private ApplicationContext applicationContext;

	/** MessageSourceAccessor for easy message access */
	private MessageSourceAccessor messageSourceAccessor;

	public void setApplicationContext(ApplicationContext context)
			throws BeansException {
		if (context == null && !isContextRequired()) {
			// Reset internal context state.
			this.applicationContext = null;
			this.messageSourceAccessor = null;
		} else if (this.applicationContext == null) {
			// Initialize with passed-in context.
			if (!requiredContextClass().isInstance(context)) {
				throw new ApplicationContextException(
						"Invalid application context: needs to be of type ["
								+ requiredContextClass().getName() + "]");
			}
			this.applicationContext = context;
			this.messageSourceAccessor = new MessageSourceAccessor(context);
			initApplicationContext();
		} else {
			// Ignore reinitialization if same context passed in.
			if (this.applicationContext != context) {
				throw new ApplicationContextException(
						"Cannot reinitialize with different application context: current one is ["
								+ this.applicationContext
								+ "], passed-in one is [" + context + "]");
			}
		}
	}

	/**
	 * Determine whether this application object needs to run in an
	 * ApplicationContext.
	 * <p>
	 * Default is "false". Can be overridden to enforce running in a context
	 * (i.e. to throw IllegalStateException on accessors if outside a context).
	 * 
	 * @see #getApplicationContext
	 * @see #getMessageSourceAccessor
	 */
	protected boolean isContextRequired() {
		return false;
	}

	/**
	 * Determine the context class that any context passed to
	 * <code>setApplicationContext</code> must be an instance of. Can be
	 * overridden in subclasses.
	 * 
	 * @see #setApplicationContext
	 */
	protected Class requiredContextClass() {
		return ApplicationContext.class;
	}

	/**
	 * Subclasses can override this for custom initialization behavior. Gets
	 * called by <code>setApplicationContext</code> after setting the context
	 * instance.
	 * <p>
	 * Note: Does </i>not</i> get called on reinitialization of the context but
	 * rather just on first initialization of this object's context reference.
	 * 
	 * @throws ApplicationContextException
	 *             in case of initialization errors
	 * @throws BeansException
	 *             if thrown by ApplicationContext methods
	 * @see #setApplicationContext
	 */
	protected void initApplicationContext() throws BeansException {
	}

	/**
	 * Return the ApplicationContext that this object is associated with.
	 * 
	 * @throws IllegalStateException
	 *             if not running in an ApplicationContext
	 */
	public ApplicationContext getApplicationContext()
			throws IllegalStateException {
		if (this.applicationContext == null && isContextRequired()) {
			throw new IllegalStateException(
					"ApplicationObjectSupport instance [" + this
							+ "] does not run in an ApplicationContext");
		}
		return this.applicationContext;
	}

	/**
	 * Return a MessageSourceAccessor for the application context used by this
	 * object, for easy message access.
	 * 
	 * @throws IllegalStateException
	 *             if not running in an ApplicationContext
	 */
	protected MessageSourceAccessor getMessageSourceAccessor()
			throws IllegalStateException {
		if (this.messageSourceAccessor == null && isContextRequired()) {
			throw new IllegalStateException(
					"ApplicationObjectSupport instance [" + this
							+ "] does not run in an ApplicationContext");
		}
		return this.messageSourceAccessor;
	}

}
